//
//  ConYueViewController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/7/12.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConYueViewController.h"
#import "HomeYueViewCell.h"
#import <MJRefresh.h>
#import "ClubNetwork.h"
#import "MBProgressHUD+MJ.h"
#import "ConNearActivityDetailController.h"
#import "ConNearAcInfoViewController.h"
#import "ConOldActivityDetailController.h"
#import "ConeBaseViewController.h"
#import "NewMapModel.h"
#import "NSDate+Extension.h"
#import "SZLAccountHelper.h"
#import "AfterTeaVc.h"

@interface ConYueViewController ()

@property (nonatomic,assign) NSInteger curPage;
@property (nonatomic,strong) NSMutableArray *sources;

@end

@implementation ConYueViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (self.type == 0) {
        self.title = @"全部活动";
    }else  if (self.type == 1) {
        self.title = @"下午茶";
    }else if (self.type == 2) {
        self.title = @"结缘晚宴";
    }else if (self.type == 3) {
        self.title = @"平台活动";
    }else if (self.type == 4) {
        self.title = @"路演大会";
    }
    self.curPage = 1;
    [self setUpTableView];
    [self.tableView.mj_header beginRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sources.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 32+(ScreenW*230/750)*146/230;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeYueViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeYueViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.sources[indexPath.row];
    cell.type = self.type;
    cell.reportBlock = ^(HomeYueModel *model) {
        [self reportClick:model];
    };
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeYueModel *model = self.sources[indexPath.row];
    if ([model.NewType isEqualToString:@"3"]) {//平台基地活动
        ConeBaseViewController *vc = [[ConeBaseViewController alloc]init];
        NewMapModel *mapModel = [[NewMapModel alloc]init];
        mapModel.ID = model.clubId;
        mapModel.clubType = 2;//基地
        vc.mapModel = mapModel;
        vc.activityId = [model.ID integerValue];//活动id
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        if ([model.type isEqualToString:@"2"]) {
            ConOldActivityDetailController *detailVc = [[ConOldActivityDetailController alloc]init];
            detailVc.ID = [model.ID integerValue];
            if (model.videoUrl.length > 0) {
                detailVc.videoURL = [NSURL URLWithString:model.videoUrl];
                detailVc.videoName = model.videoName;
                detailVc.avatarUrl = model.activityPictureUrl;
            }
            [self.navigationController pushViewController:detailVc animated:YES];
        }else {
            if ([model.NewType isEqualToString:@"1"]||[model.NewType isEqualToString:@"2"]) {
                AfterTeaVc *vc = [[AfterTeaVc alloc]init];
                vc.ID = model.ID;
                vc.type = [model.NewType integerValue];
                [self.navigationController pushViewController:vc animated:YES];
            }else {
                ConNearAcInfoViewController *infoVc = [[ConNearAcInfoViewController alloc]init];
                infoVc.ID = [model.ID integerValue];
                [self.navigationController pushViewController:infoVc animated:YES];
            }
        }
    }
}
- (void)setUpTableView
{
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 0);
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeYueViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([HomeYueViewCell class])];
    self.tableView.contentInset = UIEdgeInsetsMake(-2, 0, 0, 0);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.curPage = 1;
        [self.sources removeAllObjects];
        [self getYueDetail];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.curPage++;
        [self getYueDetail];
    }];
    self.tableView.mj_footer.hidden = YES;
}
- (void)getYueDetail
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"pageNo"] = @(self.curPage);
    parame[@"newType"] = @(self.type);
    [ClubNetwork getHomeYueParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self hideNoDataLab];
        if (success) {
            self.tableView.mj_footer.hidden = (lists.count < 20);
            if (self.curPage == 1&&lists.count == 0) {
                [self showNoDataLab:self.tableView range:CGRectMake(0, (ScreenH-153)*0.5, ScreenW, 20) title:@"暂无活动"];
                return ;
            }else if (lists.count>=20) {
                self.tableView.mj_footer.hidden = NO;
            }
            [self.sources addObjectsFromArray:lists];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}

//活动报名
- (void)reportClick:(HomeYueModel *)model
{
    if ([SZLAccountHelper shouldLogin]) return;
    NSDate *holdDate = [NSDate stringToDate:[NSString stringWithFormat:@"%lld",[model.startDate longLongValue]/1000]];
    BOOL isPast = [holdDate isInPast];
    if (isPast) {
        [MBProgressHUD showError:@"活动已举办完成" toView:self.view];
        return;
    }
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"activityId"] = model.ID;
    parame[@"registrationName"] = [SZLAccountHelper shareInstance].name;
    parame[@"registrationPhone"] = [SZLAccountHelper shareInstance].mobilePhone;
    [self showClearBgWaiter];
    [ClubNetwork nearActivityReportWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"报名成功"];
            model.applyCount += 1;
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}

- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}


@end
